package com.pshcp.patient.tool;

import com.external.pay.domain.PayReqDTO;
import com.external.ws.soap.entity.dto.PayClinicConfirmDTO;
import com.external.ws.soap.entity.dto.PayClinicConfirmItem;
import com.external.ws.soap.entity.dto.UnpaidFeeListDTO;
import com.external.ws.soap.entity.dto.UnpaidFeeViewDTO;
import com.external.ws.soap.entity.res.UnpaidFeeListRes;
import com.external.ws.soap.entity.res.UnpaidFeeViewRes;
import com.framework.base.TwoDTO;
import com.framework.thread.ReqComHold;
import com.framework.tool.CommonTool;
import com.pshcp.common.enums.PayBusinessEnum;
import com.pshcp.common.enums.PayModeEnum;
import com.pshcp.common.enums.RegPayStatusEnum;
import com.pshcp.domain.model.Patient;
import com.pshcp.domain.model.PatientUnpayFeeRecord;
import com.pshcp.patient.domain.patientUnpaid.ao.UnpaidListAO;
import com.pshcp.patient.domain.patientUnpaid.ao.UnpaidToPayAO;
import com.pshcp.patient.domain.patientUnpaid.vo.UnpaidDetailItemVO;
import com.pshcp.patient.domain.patientUnpaid.vo.UnpaidDetailVO;
import com.pshcp.patient.domain.patientUnpaid.vo.UnpaidListVO;
import com.pshcp.patient.domain.patientUnpaid.vo.UnpaidOrderVO;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.List;

public class PatientUnpaidTools {

    /**
     * build  ws dto
     * @param patient
     * @return
     */
    public static UnpaidFeeListDTO buildWsListDTO(Patient patient, UnpaidListAO listAO){
        UnpaidFeeListDTO dto = new UnpaidFeeListDTO();
        dto.setPatName(patient.getName());
        dto.setIdCardNo(patient.getIdCardNo());
        dto.setPatCardNo(patient.getPatientCardNo());
        dto.setAccessPatId(patient.getHisPatientId());
        dto.setPatCardType(patient.getPatientCardType());
        TwoDTO<String, String> startEndTime = PatientCommonTools.buildStartEndTime(listAO.getSearchTime());
        dto.setStartDate(startEndTime.getOne());
        dto.setEndDate(startEndTime.getTwo());
        //以下一定要给空或者给值
        dto.setPhoneNo("");
        dto.setHosDistId("");
        dto.setAccUnpaidFeeId("");
        return dto;
    }

    /**
     * build list vo
     * @param res
     * @return
     */
    public static UnpaidListVO buildListVO(UnpaidFeeListRes res){
        UnpaidListVO listVO = CommonTool.copyProperties(res, UnpaidListVO.class);
        if(CollectionUtils.isEmpty(res.getOrders())){
            return listVO;
        }
        List<UnpaidOrderVO> orders = CommonTool.copyPropertiesList(res.getOrders(), UnpaidOrderVO.class);
        listVO.setOrders(orders);
        return listVO;
    }

    /**
     * build ws detail dto
     * @param hisPatientId
     * @param unpaidFeeId
     * @return
     */
    public static UnpaidFeeViewDTO buildWsViewDTO(String hisPatientId, String unpaidFeeId){
        UnpaidFeeViewDTO dto = new UnpaidFeeViewDTO();
        dto.setAccessPatId(hisPatientId);
        dto.setAccUnpaidFeeId(unpaidFeeId);
        return dto;
    }

    /**
     * build detail vo
     * @param res
     * @return
     */
    public static UnpaidDetailVO buildDetailVO(UnpaidFeeViewRes res){
        UnpaidDetailVO detailVO = CommonTool.copyProperties(res, UnpaidDetailVO.class);
        if(CollectionUtils.isEmpty(detailVO.getItems())){
            return detailVO;
        }
        List<UnpaidDetailItemVO> itemVOS = CommonTool.copyPropertiesList(detailVO.getItems(), UnpaidDetailItemVO.class);
        detailVO.setItems(itemVOS);
        return detailVO;
    }

    public static PatientUnpayFeeRecord buildCreateModel(Patient patient, UnpaidFeeViewRes res){
        PatientUnpayFeeRecord record = new PatientUnpayFeeRecord();
        record.setPatientId(patient.getId());
        record.setHisPatientId(patient.getHisPatientId());
        record.setPatientUserId(ReqComHold.getReqCom().getUserId());
        record.setUnpayFeeId(res.getAccUnpaidFeeId());
        record.setTotalFee(res.getTotalCost());
        record.setPayStatus(RegPayStatusEnum.WAIT_PAY.getCode());
        return record;
    }

    public static PayReqDTO buildPayDTO(PatientUnpayFeeRecord unpayFeeRecord, UnpaidToPayAO toPayAO){
        PayReqDTO payReqDTO = new PayReqDTO();
        payReqDTO.setPayMode(toPayAO.getPayMode());
        payReqDTO.setWechatType(toPayAO.getWechatType());
        payReqDTO.setOpenid(toPayAO.getOpenid());
        payReqDTO.setOrderId(unpayFeeRecord.getId());
        payReqDTO.setPayBusiness(PayBusinessEnum.UNPAID_FEE_PAY.getCode());
        payReqDTO.setAmount(unpayFeeRecord.getTotalFee());
        payReqDTO.setPatientUserId(unpayFeeRecord.getPatientUserId());
        return payReqDTO;
    }

    /**
     * his 1支付宝，2微信支付，3微信公众号支付
     * @param payMode
     * @return
     */
    private static Integer getPayBy(Integer payMode){
        if(PayModeEnum.WECHAT_PAY.getCode().equals(payMode)){
            return 2;
        }
        if(PayModeEnum.ALI_PAY.getCode().equals(payMode)){
            return 1;
        }
        return 1;
    }

    public static PayClinicConfirmDTO buildWsPayConfirm(Patient patient,PatientUnpayFeeRecord feeRecord,
                                                        UnpaidFeeViewRes res,Integer payMode,String thirdNo){
        String thirdTradeNo = thirdNo;
        if(ObjectUtils.isEmpty(thirdTradeNo)){
            thirdTradeNo = feeRecord.getId()+"";
        }
        PayClinicConfirmDTO dto = new PayClinicConfirmDTO();
        dto.setYuntaiTradeNo(feeRecord.getId()+"");
        dto.setThridTradeNo(thirdTradeNo);
        dto.setAccessPatId(feeRecord.getHisPatientId());
        dto.setPayBy(getPayBy(payMode));
        dto.setAcsBizId(feeRecord.getUnpayFeeId());
        dto.setAmount(feeRecord.getTotalFee());
        dto.setPatCardNo(patient.getPatientCardNo());

        List<PayClinicConfirmItem> items = new ArrayList<>();
        res.getItems().forEach(item -> {
            PayClinicConfirmItem confirmItem = new PayClinicConfirmItem();
            confirmItem.setAcsBizItemId(item.getAccUnpaidFeeItemId());
            confirmItem.setItemPrice(item.getItemPrice());
            items.add(confirmItem);
        });

        dto.setItems(items);
        return dto;
    }

}
